﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Projekt
{
    class Produce
    {
        Dalgen conn = Dalgen.Instance;
        public bool add(String Name, String UM)
        {
            try
            {
                String sqlQuery = "Select ID from Mertekegyseg where MNev = '" + UM + "'";
                int id = (int)conn.ScalarCmd(sqlQuery);
                sqlQuery = "Insert into Aruk (ANev,AMertekegyseg) values('" + Name + "','" + id + "')";
                if (!conn.nonQueryCmd(sqlQuery)) return false;
                return true;
            }
            catch (Exception es) { throw es; }
        }

        public DataTable getProduceList()
        {
            String sqlQuery = "Select * from Aruk";
            return conn.QueryCmd(sqlQuery);
        }
        
        public bool DeleteProduce(String name)
        {
            try
            {
                string sqlQuery = "Delete from Aruk where ANev = '" + name + "'";
                return (conn.nonQueryCmd(sqlQuery));
            }
            catch (Exception es) { /*throw (es);*/ return false; }
            
        }

        public int getID(String Name, String UM)
        {
            try
            {
                String sqlQuery = "Select ID from Mertekegyseg where MNev = '" + UM + "'";
                int umID = (int)conn.ScalarCmd(sqlQuery);
                sqlQuery = "Select AruID From Aruk Where ANev = '" + Name + "' AND AMertekegyseg = '" + umID + "'";
                Object aruID = conn.ScalarCmd(sqlQuery);
                if (aruID == null)
                {
                    sqlQuery = "Insert into Aruk(ANev,AMertekegyseg) values('" + Name + "','" + umID + "')";
                    conn.nonQueryCmd(sqlQuery);
                    sqlQuery = "Select AruID From Aruk Where ANev = '" + Name + "' AND AMertekegyseg = '" + umID + "'";
                    return (int)conn.ScalarCmd(sqlQuery);
                }
                return (int)aruID;
                
            }
            catch (Exception es) { throw es; }
        }
    }
}
